package com.kingdee.eas.custom.ws.bean;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.query.BizEnumValueDTO;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.OrgUnitInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.scm.ws.app.json.BOSUuidTypeAdapter;
import com.kingdee.eas.scm.ws.app.json.BosObjectCollectionTypeAdapter;
import com.kingdee.eas.scm.ws.app.json.BosObjectTypeAdapter;
import com.kingdee.eas.scm.ws.app.json.DateTypeAdapter;
import com.kingdee.eas.scm.ws.app.json.EnumTypeAdapter;

public abstract class JsonUtils {
 
    public static String convertObjectToJson(Context ctx, Object object) throws BOSException {
        if ((object == null) || (object.equals(""))) {
            return "\"\"";
        }
        Gson gson = getGson(ctx);
        String result = gson.toJson(object);
        if (result == null) {
            return "\"\"";

        }
        result = processJsonResult(result);
        return result;
    }

    public static String processJsonResult(String result) {
        result = result.replace("${quote_2312415321232}", "\"");
        result = result.replace("\"${leftbracket_2312415321232}", "{");
        result = result.replace("${leftbracket_2312415321232}", "{");
        result = result.replace("${rightbracket_2312415321232}\"", "}");
        result = result.replace("${rightbracket_2312415321232}", "}");
        return result;
    }

    public static <T> T convertJsonToObject(Context ctx, String json, Class<T> clz) throws BOSException {
        Gson gson = getGson(ctx);
        return gson.fromJson(json, clz);
    }

    public static Map<String, Object> convertJsonToObject(Context ctx, String json) throws BOSException {
        Gson gson = getGson(ctx);
        Type type = new TypeToken<Map<String, String>>(){}.getType();
        return gson.fromJson(json.toString(), type);
    }

    public static String encodeJson(String json) throws BOSException {
        StringBuilder encodedJson = new StringBuilder();
        for (int i = 0; i < json.length(); ++i) {
            char c = json.charAt(i);
            if (c > '')
                encodedJson.append("\\u").append(Integer.toHexString(c));
            else {
                encodedJson.append(c);
            }
        }
        return encodedJson.toString();
    }

    public static Gson getGson(Context ctx) {
        GsonBuilder builder = new GsonBuilder();
        // builder.serializeNulls();
        builder.serializeSpecialFloatingPointValues();
        builder.registerTypeHierarchyAdapter(Date.class, new DateTypeAdapter());
        builder.registerTypeHierarchyAdapter(Timestamp.class, new DateTypeAdapter());
        builder.registerTypeHierarchyAdapter(BOSUuid.class, new BOSUuidTypeAdapter());
        builder.registerTypeHierarchyAdapter(CoreBaseInfo.class, new BosObjectTypeAdapter(ctx));
        builder.registerTypeHierarchyAdapter(IObjectCollection.class, new BosObjectCollectionTypeAdapter(ctx));
        builder.registerTypeHierarchyAdapter(OrgUnitInfo.class, new BosObjectTypeAdapter(ctx));
        builder.registerTypeHierarchyAdapter(BizEnumValueDTO.class, new EnumTypeAdapter(ctx));
        builder.registerTypeHierarchyAdapter(Enum.class, new EnumTypeAdapter(ctx));
        return builder.create();
    }
}
